

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="C语言在复试中的常见问题03">
  <meta name="author" content="叶尘">
  <meta name="keywords" content="">
  
  <title>复试-C语言程序设计面试问答04 - 叶尘的技术升级小木屋</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/daziyanse.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.yechen-blog.cn","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>YECHEN</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-qrcode"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/playlist/">
                <i class="iconfont icon-music"></i>
                音乐
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                链接
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    主题博客
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/guide/">
                    
                    配置指南
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    图标用法
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://www.bilibili.com">
                    
                    哔哩哔哩
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/banner.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="复试-C语言程序设计面试问答04">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      叶尘
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2023-03-14 15:57" pubdate>
        星期二, 三月 14日 2023, 3:57 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      26
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">复试-C语言程序设计面试问答04</h1>
            
            <div class="markdown-body">
              <h3 id="34、数组名是什么"><a href="#34、数组名是什么" class="headerlink" title="34、数组名是什么"></a>34、数组名是什么</h3><blockquote>
<p>数组名是一个<strong><font color=red>指向数组第一个元素的指针</font></strong>，也就是一个地址常量</p>
</blockquote>
<h3 id="35、‘c’-和-“c”-的区别"><a href="#35、‘c’-和-“c”-的区别" class="headerlink" title="35、‘c’ 和 “c” 的区别"></a>35、‘c’ 和 “c” 的区别</h3><blockquote>
<p>‘c’是字符常量，用 char 类型表示，只有一个字符</p>
<p>“c”是字符串常量，用 char 类型数组表示，包括 <code>c</code>与 <code>\0</code> 两个字符</p>
</blockquote>
<h3 id="36、为什么字符串数组的长度要比存放的字符数多-1"><a href="#36、为什么字符串数组的长度要比存放的字符数多-1" class="headerlink" title="36、为什么字符串数组的长度要比存放的字符数多 1"></a>36、为什么字符串数组的长度要比存放的字符数多 1</h3><blockquote>
<p>因为在字符串的最后还要存放一个字符 <code>\0</code> 标示字符串的结束</p>
<p>这样做的好处是，可以轻松地<strong><font color=red>在字符数组中查找字符串的长度</font></strong>。只需要在字符数组中从第一个元素开始逐个查找，直到找到null字符为止，这个位置之前的所有字符都属于该字符串。</p>
</blockquote>
<h3 id="37、字符串的首地址是什么"><a href="#37、字符串的首地址是什么" class="headerlink" title="37、字符串的首地址是什么"></a>37、字符串的首地址是什么</h3><blockquote>
<p>即字符串中首个字符在内存中存放的地址</p>
</blockquote>
<h3 id="38、什么是宏定义"><a href="#38、什么是宏定义" class="headerlink" title="38、什么是宏定义"></a>38、什么是宏定义</h3><blockquote>
<p><strong>形式：</strong><code>#define 宏名 宏体</code></p>
<p><strong>目的：</strong>在程序中使用宏名来代替宏体，在预编译阶段，把使用宏名处的数据用宏体替换。宏定义通常用于定义常量、简化复杂的表达式或函数调用、提高代码的可读性等方面。</p>
</blockquote>
<h3 id="39、何时宏定义在程序中不进行替换"><a href="#39、何时宏定义在程序中不进行替换" class="headerlink" title="39、何时宏定义在程序中不进行替换"></a>39、何时宏定义在程序中不进行替换</h3><blockquote>
<ol>
<li><p><strong>宏定义在预处理阶段没有被定义</strong>。在程序中使用未定义的宏定义，预处理器无法将其替换为相应的字符串或表达式，编译器会报错。</p>
</li>
<li><p>宏定义使用了<strong>条件编译指令</strong>，但条件不满足。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> PI</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> PI 3.14</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br></code></pre></td></tr></table></figure>

<p>在这个例子中，如果PI已经在其他地方被定义了，条件<code>#ifndef PI</code>不满足，预处理器不会对宏定义进行替换。</p>
</li>
<li><p>宏定义使用了字符串化运算符<code>#</code>或标记粘贴运算符<code>##</code>。（不懂:sob:）</p>
</li>
</ol>
</blockquote>
<h3 id="40、宏替换和函数的区别"><a href="#40、宏替换和函数的区别" class="headerlink" title="40、宏替换和函数的区别"></a>40、宏替换和函数的区别</h3><blockquote>
<ol>
<li><strong>编译和运行时期不一样</strong>：宏替换在程序预处理阶段进行；函数是在程序运行阶段才开辟内存地址去执行</li>
<li><strong>类型检查不同</strong>：宏替换只是将宏定义中的标识符替换为相应的字符或表达式，不会进行类型检查；但函数在调用是会进行类型检查，如果类型不匹配，编译就会出错。</li>
<li><strong>可读性和可维护性不同</strong>：宏替换只能用户简单的字符串或表达式的替换；而函数通常是一个独立的代码单元，可以提高程序的可读性与可维护性。</li>
<li><strong>效率</strong>：宏替换在预处理阶段完成，可以减少一些资源的开销，故效率高。</li>
</ol>
</blockquote>
<h3 id="41、ctype-h-math-h-stdio-h-stdlib-h-string-h-各是什么库"><a href="#41、ctype-h-math-h-stdio-h-stdlib-h-string-h-各是什么库" class="headerlink" title="41、ctype.h, math.h, stdio.h, stdlib.h, string.h 各是什么库"></a>41、ctype.h, math.h, stdio.h, stdlib.h, string.h 各是什么库</h3><blockquote>
<p><strong>ctype.h</strong>：字符处理函数库</p>
<p><strong>math.h</strong>：数学处理函数库</p>
<p><strong>stdio.h</strong>：标准输入输出函数库</p>
<p><strong>stdlib.h</strong>：常用函数库</p>
<p><strong>string.h</strong>：字符串处理函数库</p>
</blockquote>
<h3 id="42、什么是条件编译"><a href="#42、什么是条件编译" class="headerlink" title="42、什么是条件编译"></a>42、什么是条件编译</h3><blockquote>
<p>条件编译是一种C语言中的<strong>编译<font color=red>预处理</font>指令</strong>，希望部分语句在满足一定条件下才去执行编译，条件编译指令主要用于在不同的编译环境下编写可移植的代码。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> defined(CONDITION)</span><br>    <span class="hljs-comment">/* Code to be compiled if CONDITION is defined */</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">elif</span> defined(OTHER_CONDITION)</span><br>    <span class="hljs-comment">/* Code to be compiled if OTHER_CONDITION is defined */</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span><br>    <span class="hljs-comment">/* Code to be compiled if neither CONDITION nor OTHER_CONDITION is defined */</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br><br></code></pre></td></tr></table></figure>
</blockquote>
<h3 id="43、什么是行指针，什么是列指针，什么是元素指针"><a href="#43、什么是行指针，什么是列指针，什么是元素指针" class="headerlink" title="43、什么是行指针，什么是列指针，什么是元素指针"></a>43、什么是行指针，什么是列指针，什么是元素指针</h3><blockquote>
<p>对于二维数组，也可以使用指针来访问数组元素</p>
<p><strong>行指针：</strong>指向数组一行的指针，可以用来遍历二维数组中一行的数据</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">int</span> arr[<span class="hljs-number">3</span>][<span class="hljs-number">4</span>] = &#123; &#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>&#125;, &#123;<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>&#125;, &#123;<span class="hljs-number">9</span>, <span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>&#125; &#125;;<br><span class="hljs-keyword">int</span> *p = arr[<span class="hljs-number">1</span>];   <span class="hljs-comment">// 定义一个指向第二行元素的指针</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">4</span>; i++) &#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>, *(p + i));  <span class="hljs-comment">// 遍历第二行元素</span><br>&#125;<br><span class="hljs-comment">// 输出：5 6 7 8</span><br></code></pre></td></tr></table></figure>

<p><strong>列指针：</strong>指向数组一列的指针，可以用来遍历二维数组中一列的数据</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">int</span> arr[<span class="hljs-number">3</span>][<span class="hljs-number">4</span>] = &#123; &#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>&#125;, &#123;<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>&#125;, &#123;<span class="hljs-number">9</span>, <span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>&#125; &#125;;<br><span class="hljs-keyword">int</span> *p = &amp;arr[<span class="hljs-number">0</span>][<span class="hljs-number">1</span>];  <span class="hljs-comment">// 定义一个指向第二列元素的指针</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">3</span>; i++) &#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>, *(p + i * <span class="hljs-number">4</span>));  <span class="hljs-comment">// 遍历第二列元素</span><br>&#125;<br><span class="hljs-comment">// 输出：2 6 10</span><br></code></pre></td></tr></table></figure>

<p><strong>元素指针：</strong>指向数组中一个元素的指针，用于访问数组的一个元素</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">int</span> arr[<span class="hljs-number">3</span>][<span class="hljs-number">4</span>] = &#123; &#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>&#125;, &#123;<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>&#125;, &#123;<span class="hljs-number">9</span>, <span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>&#125; &#125;;<br><span class="hljs-keyword">int</span> *p = &amp;arr[<span class="hljs-number">1</span>][<span class="hljs-number">2</span>];  <span class="hljs-comment">// 定义一个指向第二行第三列元素的指针</span><br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d&quot;</span>, *p);     <span class="hljs-comment">// 输出第二行第三列元素的值</span><br><span class="hljs-comment">// 输出：7</span><br></code></pre></td></tr></table></figure>
</blockquote>
<h3 id="44、指针函数、函数指针、数组指针和指针数组各是什么"><a href="#44、指针函数、函数指针、数组指针和指针数组各是什么" class="headerlink" title="44、指针函数、函数指针、数组指针和指针数组各是什么"></a>44、指针函数、函数指针、数组指针和指针数组各是什么</h3><blockquote>
<p><strong>指针函数：</strong>就是一个返回值是指针的函数</p>
<p><strong>函数指针：</strong>一个指向函数的指针，可以当做变量传入其他函数，实现函数的回调调用</p>
<p><strong>数组指针：</strong>是一个指向数组的指针，具体来说是一个指向数组首元素的指针</p>
<p><strong>指针数组：</strong>一个数组，保存的数据是指针，即内存地址</p>
</blockquote>
<h3 id="45、常用字符串处理函数的使用"><a href="#45、常用字符串处理函数的使用" class="headerlink" title="45、常用字符串处理函数的使用"></a>45、常用字符串处理函数的使用</h3><blockquote>
<p>首先要引入 <code>string.h</code> 头文件，常见函数如下：</p>
<ol>
<li>strlen(str)：返回字符串str的长度。</li>
<li>strcpy(dest, src)：将字符串src复制到dest中。</li>
<li>strcat(dest, src)：将字符串src追加到dest字符串的末尾。</li>
<li>strcmp(str1, str2)：比较两个字符串str1和str2，如果相等返回0，如果不相等返回非0值。</li>
<li>strchr(str, ch)：在字符串str中查找字符ch，如果找到返回该字符所在位置的指针，否则返回NULL。</li>
<li>strstr(str1, str2)：在字符串str1中查找字符串str2，如果找到返回str2在str1中第一次出现的位置的指针，否则返回NULL。</li>
<li>isalpha(ch)：判断字符ch是否为字母。</li>
<li>isdigit(ch)：判断字符ch是否为数字。</li>
<li>toupper(ch)：将小写字母转换为大写字母。</li>
<li>tolower(ch)：将大写字母转换为小写字母。</li>
</ol>
</blockquote>
<h3 id="46、main-函数的参数使用"><a href="#46、main-函数的参数使用" class="headerlink" title="46、main 函数的参数使用"></a>46、main 函数的参数使用</h3><blockquote>
<p>C语言中的<code>main</code>函数可以带有两个参数，分别是<code>argc</code>和<code>argv</code>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span> </span>&#123;&#125;<br></code></pre></td></tr></table></figure>

<p><code>argc</code>表示<strong>程序运行时命令行参数的个数</strong>，它是一个<strong>整数类型</strong>的变量，其值至少为1，因为第一个参数通常是程序的名称</p>
<p><code>argv</code>是一个<strong>指针数组</strong>，每个指针指向一个命令行参数字符串</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;argc = %d\n&quot;</span>, argc);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;argv[0] = %s&quot;</span>, argv[<span class="hljs-number">0</span>]);<br>&#125;<br><span class="hljs-comment">// 默认有一个参数</span><br><span class="hljs-comment">/*</span><br><span class="hljs-comment">argc = 1</span><br><span class="hljs-comment">argv[0] = D:\Desktop\testc\cmake-build-debug\testc.exe</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<p>也可以自己设置参数，每个参数用空格隔开</p>
<p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/image2/202303141524608.png" srcset="/img/loading.gif" lazyload alt="image-20230314152417535"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;argc = %d\n&quot;</span>, argc);<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; argc; ++i) &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;argv[%d] = %s\n&quot;</span>, i, argv[i]);<br>    &#125;<br>&#125;<br><span class="hljs-comment">// 输出</span><br><span class="hljs-comment">/* </span><br><span class="hljs-comment">argc = 5</span><br><span class="hljs-comment">argv[0] = D:\Desktop\testc\cmake-build-debug\testc.exe</span><br><span class="hljs-comment">argv[1] = 123</span><br><span class="hljs-comment">argv[2] = helle</span><br><span class="hljs-comment">argv[3] = world</span><br><span class="hljs-comment">argv[4] = yechen</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>
</blockquote>
<h3 id="47、结构变量引用规则-x"><a href="#47、结构变量引用规则-x" class="headerlink" title="47、结构变量引用规则:x:"></a>47、结构变量引用规则:x:</h3><h3 id="48、如何创建链表？如何在链表中删除、遍历、增加新结点"><a href="#48、如何创建链表？如何在链表中删除、遍历、增加新结点" class="headerlink" title="48、如何创建链表？如何在链表中删除、遍历、增加新结点"></a>48、如何创建链表？如何在链表中删除、遍历、增加新结点</h3><blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">// 建立一个结点</span><br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> &#123;</span><br>    <span class="hljs-keyword">int</span> data;<br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> *<span class="hljs-title">next</span>;</span><br>&#125;Node;<br></code></pre></td></tr></table></figure>
</blockquote>
<h3 id="49、malloc-和-calloc-函数的使用"><a href="#49、malloc-和-calloc-函数的使用" class="headerlink" title="49、malloc 和 calloc 函数的使用"></a>49、malloc 和 calloc 函数的使用</h3><blockquote>
<p><code>malloc</code> 和 <code>calloc</code> 函数均可以动态分配内存空间，不同之处在于 <code>calloc</code> 函数会将分配的内存看空间初始化为 0，<code>malloc</code> 不会</p>
<p><code>malloc</code> 函数原型</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span>* <span class="hljs-title">malloc</span><span class="hljs-params">(<span class="hljs-keyword">size_t</span> size)</span></span>;<br></code></pre></td></tr></table></figure>

<p>使用</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">int</span>* p;<br>p = (<span class="hljs-keyword">int</span>*)<span class="hljs-built_in">malloc</span>(<span class="hljs-number">10</span> * <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>)); <span class="hljs-comment">// 分配10个int类型的空间</span><br><span class="hljs-keyword">if</span> (p == <span class="hljs-literal">NULL</span>) &#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;内存分配失败！&quot;</span>);<br>    <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);<br>&#125;<br><span class="hljs-comment">// 对p指向的空间进行操作</span><br><span class="hljs-built_in">free</span>(p); <span class="hljs-comment">// 释放p指向的空间</span><br></code></pre></td></tr></table></figure>

<p><code>calloc</code> 函数原型</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span>* <span class="hljs-title">calloc</span><span class="hljs-params">(<span class="hljs-keyword">size_t</span> num, <span class="hljs-keyword">size_t</span> size)</span></span>;<br></code></pre></td></tr></table></figure>

<p>使用</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">int</span>* p;<br>p = (<span class="hljs-keyword">int</span>*)<span class="hljs-built_in">calloc</span>(<span class="hljs-number">10</span>, <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>)); <span class="hljs-comment">// 分配10个int类型的空间，且每个空间都初始化为0</span><br><span class="hljs-keyword">if</span> (p == <span class="hljs-literal">NULL</span>) &#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;内存分配失败！&quot;</span>);<br>    <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);<br>&#125;<br><span class="hljs-comment">// 对p指向的空间进行操作</span><br><span class="hljs-built_in">free</span>(p); <span class="hljs-comment">// 释放p指向的空间</span><br></code></pre></td></tr></table></figure>


</blockquote>
<h3 id="50、定义共用体的三种方法"><a href="#50、定义共用体的三种方法" class="headerlink" title="50、定义共用体的三种方法"></a>50、定义共用体的三种方法</h3><blockquote>
<p><strong>直接定义共用体变量：</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-class"><span class="hljs-keyword">union</span> <span class="hljs-title">Data</span> &#123;</span><br>   <span class="hljs-keyword">int</span> i;<br>   <span class="hljs-keyword">float</span> f;<br>   <span class="hljs-keyword">char</span> str[<span class="hljs-number">20</span>];<br>&#125; data;<br></code></pre></td></tr></table></figure>

<p><strong>声明共用体类型，再定义共用体变量：</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-class"><span class="hljs-keyword">union</span> <span class="hljs-title">Data</span> &#123;</span><br>   <span class="hljs-keyword">int</span> i;<br>   <span class="hljs-keyword">float</span> f;<br>   <span class="hljs-keyword">char</span> str[<span class="hljs-number">20</span>];<br>&#125;;<br><span class="hljs-class"><span class="hljs-keyword">union</span> <span class="hljs-title">Data</span> <span class="hljs-title">data</span>;</span><br></code></pre></td></tr></table></figure>

<p><strong>匿名共用体：</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Book</span> &#123;</span><br>    <span class="hljs-keyword">char</span> title[<span class="hljs-number">50</span>];<br>    <span class="hljs-keyword">char</span> author[<span class="hljs-number">50</span>];<br>    <span class="hljs-class"><span class="hljs-keyword">union</span> &#123;</span><br>        <span class="hljs-keyword">float</span> price;<br>        <span class="hljs-keyword">int</span> pages;<br>    &#125;;<br>&#125; book;<br></code></pre></td></tr></table></figure>


</blockquote>
<h3 id="51、什么是文本文件？什么是二进制文件"><a href="#51、什么是文本文件？什么是二进制文件" class="headerlink" title="51、什么是文本文件？什么是二进制文件"></a>51、什么是文本文件？什么是二进制文件</h3><blockquote>
<p>文本文件和二进制文件的区别在于<strong>数据的存储方式和读取方式</strong>。</p>
<p><strong>文本文件</strong>以文本格式编码，数据可直接读取和解释。</p>
<p><strong>二进制文件</strong>以二进制格式存储，数据需要进行解析和转换才能读取和使用。</p>
</blockquote>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E8%80%83%E7%A0%94/">考研</a>
                    
                      <a class="hover-with-bg" href="/categories/%E8%80%83%E7%A0%94/%E5%A4%8D%E8%AF%95/">复试</a>
                    
                      <a class="hover-with-bg" href="/categories/%E8%80%83%E7%A0%94/%E5%A4%8D%E8%AF%95/C%E8%AF%AD%E8%A8%80/">C语言</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E8%80%83%E7%A0%94/">考研</a>
                    
                      <a class="hover-with-bg" href="/tags/%E5%A4%8D%E8%AF%95/">复试</a>
                    
                      <a class="hover-with-bg" href="/tags/C%E8%AF%AD%E8%A8%80/">C语言</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/03/10/%E5%A4%8D%E8%AF%95-%E5%A4%8D%E8%AF%95-C%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E9%9D%A2%E8%AF%95%E9%97%AE%E7%AD%9403/">
                        <span class="hidden-mobile">复试-C语言程序设计面试问答03</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>














  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

  <!--动态线条背景-->
  <script type="text/javascript" color="70,119,184" opacity='1' zIndex="-2" count="200" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":200,"height":400},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
